package com.fanttec.common.websocket.config;

import cn.hutool.core.util.StrUtil;
import com.fanttec.common.websocket.config.properties.WebSocketProperties;
import com.fanttec.common.websocket.handler.PlusWebSocketHandler;
import com.fanttec.common.websocket.interceptor.PlusWebSocketInterceptor;
import com.fanttec.common.websocket.interceptor.WebSocketChannelInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.*;

import java.util.List;

@Configuration
@ConditionalOnProperty(value = "websocket.enabled", havingValue = "true")
@EnableConfigurationProperties(WebSocketProperties.class)
@EnableWebSocketMessageBroker
public class WebSocketAutoConfig implements WebSocketConfigurer,WebSocketMessageBrokerConfigurer {
    private final WebSocketProperties properties;
    WebSocketAutoConfig( WebSocketProperties webSocketProperties){
        this.properties = webSocketProperties;
    }
    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic"); // 定义消息代理的前缀
        config.setApplicationDestinationPrefixes("/app"); // 定义应用程序的前缀
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 如果WebSocket的路径为空，则设置默认路径为 "/tower"
        if (StrUtil.isBlank(this.properties.getPath())) {
            this.properties.setPath("/tower");
        }

        // 如果允许跨域访问的地址为空，则设置为 "*"，表示允许所有来源的跨域请求
        if (StrUtil.isBlank(this.properties.getAllowedOrigins())) {
            this.properties.setAllowedOrigins("*");
        }
        registry.addEndpoint(this.properties.getPath()).addInterceptors(new PlusWebSocketInterceptor());
    }

    /**
     * 配置发送与接收的消息参数，可以指定消息字节大小，缓存大小，发送超时时间
     *
     * @param registration
     */
    @Override
    public void configureWebSocketTransport(WebSocketTransportRegistration registration) {
        /*
         * 1. setMessageSizeLimit 设置消息缓存的字节数大小 字节
         * 2. setSendBufferSizeLimit 设置websocket会话时，缓存的大小 字节
         * 3. setSendTimeLimit 设置消息发送会话超时时间，毫秒
         */
        registration.setMessageSizeLimit(10240)
                .setSendBufferSizeLimit(10240)
                .setSendTimeLimit(10000);
    }

    @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {
        registration.interceptors(new WebSocketChannelInterceptor());
    }
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(new PlusWebSocketHandler(),"/tower/simple");
    }
}
